﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations;
using WalkingTec.Mvvm.Core;
using WalkingTec.Mvvm.Core.Extensions;
using AssetManageSystem.Model.AssetManage;
using AssetManageSystem.ViewModel.AssetManage.AssetInfoVMs;
using AssetManageSystem.Model.OrgManage;
using AssetManageSystem.Model;
using AssetManageSystem.Model.Enum;

namespace AssetManageSystem.ViewModel.AssetManage.AssetRepairVMs
{
    public partial class AssetRepairVM : BaseCRUDVM<AssetRepair>
    {
        public List<ComboSelectListItem> AllRepairAssets { get; set; }
        public List<ComboSelectListItem> AllRepairmans { get; set; }
        public List<ComboSelectListItem> AllRepairDeps { get; set; }
        public AssetInfoListVM AssetInfoList { get; set; }

        public AssetRepairVM()
        {
            SetInclude(x => x.RepairAsset);
            SetInclude(x => x.Repairman);
            SetInclude(x => x.RepairDep);
        }

        protected override void InitVM()
        {
            //获取并设置资产列表
            AssetInfoList = new AssetInfoListVM();
            AssetInfoList.CopyContext(this);

            AllRepairAssets = DC.Set<AssetInfo>().GetSelectListItems(Wtm, y => y.AsName);
            AllRepairmans = DC.Set<User>().GetSelectListItems(Wtm, y => y.UserName);
            AllRepairDeps = DC.Set<Department>().GetSelectListItems(Wtm, y => y.DepName);
            Entity.DocNumber = "WX" + Helper.RandomNumber.GetRandom();
        }

        public override void DoAdd()
        {
            var AssetInfos = DC.Set<AssetInfo>().Where(x => x.ID == Entity.RepairAssetId).Select(x => x).ToList();
            if (AssetInfos != null)
            {
                var info = AssetInfos.Where(x => x.ID == Entity.RepairAssetId).FirstOrDefault();
                if (info != null)
                {
                    info.Status = AsStatusEnum.Repair;
                }
                DC.SaveChanges();
            }
            base.DoAdd();
        }
        public override void DoEdit(bool updateAllFields = false)
        {
            base.DoEdit(updateAllFields);
        }

        public override void DoDelete()
        {
            base.DoDelete();
        }
    }
}